package com.comp3111.happymeal;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;

/*
 * this download images task will load the picture into a view as Background (for restaurant and restaurant food only)
 */

@SuppressLint("NewApi")
public class DownloadImagesTask extends AsyncTask<View, Void, Bitmap> {

    View imageView = null;
    
    @Override
	protected void onPreExecute() {
		// update the UI immediately after the task is executed
		super.onPreExecute();
		
	}
    
    @Override
    protected Bitmap doInBackground(View... imageViews) {
        this.imageView = imageViews[0];
        return download_Image((String)imageView.getTag());
    }

    @Override
    protected void onPostExecute(Bitmap result) {
    	Drawable drawable = new BitmapDrawable(result);
    	
        imageView.setBackground(drawable);
    }

    private Bitmap download_Image(String url) {
        Bitmap bmp =null;
        try{
            URL ulrn = new URL(url);
            HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
            InputStream is = con.getInputStream();
            bmp = BitmapFactory.decodeStream(is);
            if (null != bmp)
                return bmp;

            }catch(Exception e){}
        return bmp;
    }
}